﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using FileManager_Client.PresentationLayer.Common;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class FileManager_Form
    {
        #region Download Log Shown Event
        private List<DownloadLogView> downloadLogViewCollection = new List<DownloadLogView>();
        void FileManager_Form_Shown_DownloadLog(object sender, EventArgs e)
        {
            var logCollection = UserLocalFile.GetFileDownloadLogObject();
            if(logCollection.Count <= 0)return;
            downloadLogViewCollection.AddRange(logCollection);
            Olv_DownloadLog.SetObjects(logCollection);
        }
        #endregion

        #region Add Download Log Method
        private void AddDownloadLog(DownloadingFileProgress downloadingFileProgress)
        {
            var addDwonloadLog = new DownloadLogView
                                     {
                                         BeginTime = downloadingFileProgress.BeginTime,
                                         DisplaySize = downloadingFileProgress.DisplaySize,
                                         EndTime = DateTime.Now,
                                         FileGuid = downloadingFileProgress.FileGuid,
                                         FileLength = downloadingFileProgress.FileSize,
                                         FileName = downloadingFileProgress.FileName,
                                         FolderId = downloadingFileProgress.FolderId,
                                         LocalPath = downloadingFileProgress.FileLocalPath,
                                         ServerPath = downloadingFileProgress.ServerPath
                                     };
            UserLocalFile.SaveFileDownloadLogObject(addDwonloadLog);
            downloadLogViewCollection.Add(addDwonloadLog);
            Olv_DownloadLog.AddObject(addDwonloadLog);
            Olv_DownloadLog.RefreshObject(addDwonloadLog);
        }
        #endregion

        #region DownloadLog Context Menum Event
        private void tsmi_OpenDownloadServerFolder_Click(object sender, EventArgs e)
        {
            if (null == Olv_DownloadLog.SelectedObject) return;
            var logObject = Olv_DownloadLog.SelectedObject as DownloadLogView;
            if (null == logObject) return;
            ToolBar_System.SelectedIndex = ToolBarItemIndex.FileManager;
            UpdateListView(logObject.FolderId);
        }

        private void tsmi_OpenDownloadLocalFolder_Click(object sender, EventArgs e)
        {
            if (null == Olv_DownloadLog.SelectedObject) return;
            var logObject = Olv_DownloadLog.SelectedObject as DownloadLogView;
            if (null == logObject) return;
            if (string.IsNullOrEmpty(logObject.LocalPath)) return;
            Process.Start("Explorer.exe", "/select," + logObject.LocalPath);
        }

        private void tsmi_DeleteDownloadLog_Click(object sender, EventArgs e)
        {
            if (null == Olv_DownloadLog.SelectedObject) return;
            var logObject = Olv_DownloadLog.SelectedObject as DownloadLogView;
            if (null == logObject) return;
            Olv_DownloadLog.RemoveObject(logObject);
            downloadLogViewCollection.Remove(logObject);
            UserLocalFile.RemoveFileDownloadLogObject(logObject.FileGuid);
        }

        private void tsmi_DeleteDownloadLogAll_Click(object sender, EventArgs e)
        {
            var delObj = new List<DownloadLogView>();
            foreach (DownloadLogView del in Olv_DownloadLog.Objects)
            {
                UserLocalFile.RemoveFileDownloadLogObject(del.FileGuid);
                delObj.Add(del);
                downloadLogViewCollection.Remove(del);
            }
            foreach (var downloadFileLog in delObj)
                Olv_DownloadLog.RemoveObject(downloadFileLog);
        }

        private void cms_DownloadLog_Opening(object sender, CancelEventArgs e)
        {
            tsmi_OpenDownloadLocalFolder.Enabled = null != Olv_DownloadLog.SelectedItem;
            tsmi_OpenDownloadServerFolder.Enabled = null != Olv_DownloadLog.SelectedItem;
            tsmi_DeleteDownloadLog.Enabled = null != Olv_DownloadLog.SelectedItem;
            tsmi_DeleteDownloadLogAll.Enabled = Olv_DownloadLog.Items.Count > 0;
        }
        #endregion
    }

    #region Download Log Object
    [Serializable]
    public class DownloadLogView
    {
        public string FileName { get; set; }
        public DateTime BeginTime { get; set; }
        public string LocalPath { get; set; }
        public double DisplaySize { get; set; }
        public string FileGuid { get; set; }
        public string FolderId { get; set; }
        public long FileLength { get; set; }
        public string ServerPath { get; set; }
        public DateTime EndTime { get; set; }
    }
    #endregion
}